package itheima.zuoye.Test2;

import cn.hutool.core.io.FileUtil;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Scanner;

public class Test {
    public static org.slf4j.Logger log2 = LoggerFactory.getLogger(itheima.d07.Test.class);
    private static final String SOURCE_DIR = "D:\\song";
    private static final String TARGET_DIR = "E:\\songs";

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("请选择你要进行的操作：");
            System.out.println("1.查询所有歌曲");
            System.out.println("2.根据歌曲名称复制");
            System.out.println("3.根据歌曲名称删除");
            System.out.println("4.退出");
            int choice = scanner.nextInt();
            switch (choice) {
                case 1:
                    System.out.println("查询所有歌曲");
                    show();
                    log2.info("查询所有歌曲");
                    break;
                case 2:
                    System.out.println("根据歌曲名称复制");
                    copy(scanner);
                    log2.info("根据歌曲名称复制");
                    break;
                case 3:
                    System.out.println("根据歌曲名称删除");
                    remove(scanner);
                    log2.info("根据歌曲名称删除");
                    break;
                case 4:
                    System.out.println("退出");
                    return;
                default:
                    System.out.println("输入错误");
                    break;

            }
        }


    }


    public static void show() {
        //用hutool展示源文件中的歌曲
        File dir = new File(SOURCE_DIR);
        if (!dir.exists()) {
            System.out.println("目录不存在！");
            return;
        }

        File[] files = dir.listFiles();
        if (files == null || files.length == 0) {
            System.out.println("没有找到任何歌曲！");
            return;
        }

        System.out.println("存在以下歌曲：");
        for (File file : files) {
            System.out.println(file.getName());
        }
    }

    public static void copy(Scanner scanner) {
        //键盘输入歌曲名称，从源目录中复制到目标目录中
        System.out.print("请输入要复制的歌曲名称：");
        String songName = scanner.next();

        File sourceFile = new File(SOURCE_DIR, songName);
        if (!sourceFile.exists()) {
            System.out.println("歌曲不存在！");

        }

        File targetDir = new File(TARGET_DIR);
        if (!targetDir.exists()) {
            targetDir.mkdirs();
        }

        File targetFile = new File(targetDir, songName);
        try {
            Files.copy(sourceFile.toPath(), targetFile.toPath());
            System.out.println("复制结果：歌曲" + songName + "已经成功复制到" + TARGET_DIR + "目录中");
        } catch (IOException e) {
            System.out.println("复制失败：" + e.getMessage());
        }

    }

    public static void remove(Scanner scanner) {
        System.out.print("请输入要删除的歌曲名称：");
        String songName = scanner.next();

        File file = new File(SOURCE_DIR, songName);


        if (file.delete()) {
            System.out.println("删除结果：歌曲" + songName + "已经成功删除");
        } else {
            System.out.println("删除失败！");
        }
    }

}

